/* Exercise 8.3 Reaction Timer */ /* Created David Miles April 2006 */ /* Updated Ben Rowland Febuary 2014 */ #include // CONFIG1 #pragma config FOSC = HS // Oscillator Selection (HS Oscillator, High-speed crystal/resonator connected between OSC1 and OSC2 pins) #pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled) #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled) #pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR) #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) #pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled) #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled) #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) #pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled) #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled) // CONFIG2 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) #pragma config VCAPEN = OFF // Voltage Regulator Capacitor Enable (All VCAP pin functionality is disabled) #pragma config PLLEN = OFF // PLL Enable (4x PLL disabled) #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset) #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.) #pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming) #define _XTAL_FREQ 19660800 // Defines the hardware crystal frequency allowing the delay function to work correctly #define SPEED 500 // Used to trim the delay loops #include "lcdlib.h" const unsigned char banner1 [14] = {'R','e','a','c','t','i','o','n',' ','G','a','m','e',0x00}; const unsigned char banner2 [14] = {'*','*','*','*','*','*','*','*',' ','*','*','*','*',0x00}; const unsigned char start1 [] = {'P','r','e','s','s',' ','t','h','e',0x00}; const unsigned char start2 [] = {'b','u','t','t','t','o','n',' ','t','o',' ','s','t','a','r','t',0x00}; const unsigned char ready [] = {'R','e','a','d','y','.','.',0x00}; const unsigned char score [] = {'Y','o','u',' ','s','c','o','r','e','d',' ',0x00}; int time_count = 0 ; unsigned char timer_active = 0 ; unsigned char counter = 0; void setup_hardware (void) { ANSELA = 0x00; //Set PortA to use digital inputs ANSELB = 0x00; TRISA = 0x01; //PORTA bit 0 set to input, other pins set to outputs TRISB = 0x00; //PORTB set to all outputs INTCON = 0b10100000; //Configure the INTCON register to enable timer interrupts OPTION_REG = 0b01000100 ; //Configure the timer to use the prescaler } //returns the debounced state of the bottom bit of PORTA unsigned char key ( void ) { unsigned char count = 0; unsigned char oldv, newv; oldv = PORTA & 1 ; while ( count < 20 ) { newv = PORTA & 1 ; if ( oldv == newv ) { count++ ; } else { count = 0 ; oldv = newv ; } } return oldv ; } //repeatedly calls key until it is down void keywait ( void ) { while ( key() == 0 ); } //repeatedly calls key until it is up void upwait ( void ) { while ( key() == 0 ); } void tmrHandler( void ) { if (timer_active != 0) //increment the timer if it has been set to active { time_count = time_count + 1 ; } } //This is the interrupt handler It is called when the PICmicro detects an interrupt. //It checks the status bits to find out who caused the interrupt and then calls that handler void interrupt isr( void ) { if(INTCON & 0b00000100) //if the timer has overflowed bit 2 of INTCON is set high { INTCON = INTCON & 0b11111011; //clear the bit to turn off this interrupt counter = counter + 1; if ( counter > 2 ) { counter = 0; tmrHandler(); //call the handler function } } } void big_delay ( int size ) { int i, j; for (i = 0; i < size; i = i + 1) { for (j = 0; j < SPEED; j = j + 1); } } void display_value ( int value ) { unsigned char hunds, tens, units ; /* first get the digits */ units = value % 10 ; value = value / 10 ; tens = value % 10 ; value = value / 10 ; hunds = value % 10 ; /* now display them */ lcd_print_ch ( '0' + hunds ) ; lcd_print_ch ( '0' + tens ) ; lcd_print_ch ( '0' + units ) ; } int main (void) { setup_hardware(); lcd_start(); lcd_cursor (0, 0); lcd_print (banner1); lcd_cursor (0, 1); lcd_print (banner2); big_delay (1000); while (1) //Loop forever { lcd_clear (); //turn off LEDS PORTA = 0x00; lcd_print(start1); //display the start message lcd_cursor(0, 1); lcd_print(start2); keywait(); //wait for keypress lcd_clear(); big_delay(2000); PORTA = 0x1e; //light the LEDS time_count = 0; //clear the counter timer_active = 1; //start it running keywait (); //wait for the button timer_active = 0; //stop the timer lcd_clear (); display_value ( time_count ); keywait (); } return 0; }